package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.web.domain.HvFile;
import com.ruoyi.web.mapper.HvFileMapper;
import com.ruoyi.web.service.IHvFileService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 文件管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@Service
public class HvFileServiceImpl extends ServiceImpl<HvFileMapper, HvFile> implements IHvFileService {

    @Autowired
    private HvFileMapper hvFileMapper;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询文件管理列表
     *
     * @param hvFile 文件管理
     * @return 文件管理
     */
    @Override
    public List<HvFile> selectHvFileList(HvFile hvFile) {
        List<HvFile> hvFileList = hvFileMapper.selectList(buildQueryWrapper(hvFile));
        return hvFileList;
    }

    @Override
    public AjaxResult uploadFile(MultipartFile file, Integer fileType) {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            String fileSuffix = "."+fileName.substring(fileName.lastIndexOf(".") + 1);
            ajax.put("fileSuffix", fileSuffix);

            //数据库存储
            this.insertFile(filePath,fileName,url,
                    FileUtils.getName(fileName),
                    file.getOriginalFilename(),
                    fileType,fileSuffix
            );

            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    public void insertFile(String filePath,String fileName,String url,
                           String newFileName,String originalFilename,
                           Integer fileType,String fileSuffix) {
        HvFile hvFile = new HvFile();
        hvFile.setFilePath(filePath);
        hvFile.setFileName(fileName);
        hvFile.setOriginalName(originalFilename);
        hvFile.setUrl(url);
        hvFile.setFileType(fileType);
        hvFile.setNewFileName(newFileName);
        hvFile.setFileSuffix(fileSuffix);
        baseMapper.insert(hvFile);
    }


    private LambdaQueryWrapper<HvFile> buildQueryWrapper(HvFile query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<HvFile> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getFileName()), HvFile::getFileName, query.getFileName());
        lqw.like(StringUtils.isNotBlank(query.getOriginalName()), HvFile::getOriginalName, query.getOriginalName());
        lqw.eq(StringUtils.isNotBlank(query.getFileSuffix()), HvFile::getFileSuffix, query.getFileSuffix());
        lqw.eq(StringUtils.isNotBlank(query.getUrl()), HvFile::getUrl, query.getUrl());
        lqw.eq(query.getFileType() != null, HvFile::getFileType, query.getFileType());
        lqw.orderByDesc(HvFile::getCreateTime);
        return lqw;
    }

}
